package com.example.demo.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.example.demo.mapper.firstsqlserver.GroupInfoMapper;
import com.example.demo.pojo.GroupInfo;
import com.example.demo.pojo.GroupOption;
import com.fasterxml.jackson.core.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private GroupInfoMapper mapper;

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/check")
    public String check(String username, String password, Model m) {
        //System.out.println("用户名：" + username + "，密码：" + password);
        if ("admin".equals(username) && "admin2019".equals(password)) {
            List<GroupInfo> cs = mapper.listGroupInfo();
            List<GroupOption> options = new ArrayList<>();
            for (int i = 0; i < cs.size(); i++) {
                GroupOption option = new GroupOption();
                option.setValue(cs.get(i).getId() + "");
                option.setLabel(cs.get(i).getcName());
                options.add(option);
            }
            String jsonstr = JSON.toJSONString(options);
            System.out.println(jsonstr);
            //JSONArray arrays = JSONArray.parseArray(JSON.toJSONString(options), Feature.OrderedField);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time01 = sdf.format(new Date()) + " 08:00:00";
            String time02 = sdf.format(new Date()) + " 23:59:59";
            m.addAttribute("options", jsonstr);
            m.addAttribute("time01", time01);
            m.addAttribute("time02", time02);
            return "listHour";
        } else {
            return "redirect:login";
        }
    }

    @ResponseBody
    @RequestMapping("/listGroupOption")
    public List<GroupOption> listGroupOption() {
        List<GroupInfo> cs = mapper.listGroupInfo();
        List<GroupOption> options = new ArrayList<>();
        for (int i = 0; i < cs.size(); i++) {
            GroupOption option = new GroupOption();
            option.setValue(cs.get(i).getId() + "");
            option.setLabel(cs.get(i).getcName());
            options.add(option);
        }
        return options;
    }
}
